package com.bestcem.xm.user.dao.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.user.dao.UserAccountDao;
import com.bestcem.xm.user.dao.mapper.UserAccountMapper;
import com.bestcem.xm.user.entity.mysql.User;
import com.bestcem.xm.user.entity.pojo.ListUserAccountDO;
import com.bestcem.xm.user.entity.pojo.UserAccountListDO;
import com.bestcem.xm.user.entity.pojo.UserConditionGroupDO;
import com.bestcem.xm.user.entity.pojo.UserDO;
import com.bestcem.xm.user.util.business.DataBaseUtil;
import com.bestcem.xm.user.util.convert.UserConvert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author Linked <zhijian.lin@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/1 14:51
 */
@Repository
public class UserAccountDaoImpl implements UserAccountDao {

    @Resource
    private UserConvert userConvert;

    @Resource
    private UserAccountMapper userAccountMapper;

    @Override
    public String insertSelective(UserDO userDO) {
        // 设置参数
        User entity = userConvert.do2Mysql(userDO);
        entity.setUserId(StringUtils.isNotBlank(userDO.getUserId()) ? userDO.getUserId() : DataBaseUtil.generateId());
        DataBaseUtil.setCreateAndUpdateTime(entity);
        // 创建用户时，默认设置最近登录时间为创建时间
        entity.setLastLoginTime(entity.getCreateTime());
        // 新增并设置返回值
        userAccountMapper.insertSelective(entity);
        return entity.getUserId();
    }

    @Override
    public Integer checkUserByEmailAndPhone(String orgId, String phone, String email, String userId) {
        return userAccountMapper.checkUserByEmailAndPhone(orgId, phone, email, userId);
    }

    @Override
    public UserDO selectByPrimaryKey(String userId) {
        return userConvert.mysql2Do(userAccountMapper.selectByPrimaryKey(userId));
    }

    @Override
    public List<User> listByIds(String roleId, Integer status, List<String> userIds, Integer limit) {
        return userAccountMapper.listByIds(roleId, status, userIds, limit);
    }

    @Override
    public int updateByPrimaryKeySelective(User user) {
        DataBaseUtil.setUpdateTime(user);
        return userAccountMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<UserDO> selectByOrgIdSelective(UserDO user) {
        return userAccountMapper.selectByOrgIdSelective(user);
    }

    @Override
    public int updateByPrimaryKey(UserDO userDO) {
        User user = userConvert.do2Mysql(userDO);
        DataBaseUtil.setUpdateTime(user);
        return userAccountMapper.updateByPrimaryKey(user);
    }

    @Override
    public List<UserAccountListDO> getUserAccountList(ListUserAccountDO accountDO) {
        return userAccountMapper.getUserAccountList(accountDO);
    }

    @Override
    public Long getUserAccountListCount(ListUserAccountDO accountDO) {
        return userAccountMapper.getUserAccountListCount(accountDO);
    }

    @Override
    public Integer countByPhoneAndStatus(String phone, Integer status) {
        return userAccountMapper.countByPhoneAndStatus(phone, status);
    }

    @Override
    public List<UserDO> selectByIds(List<String> userIdList) {
        if(CollectionUtils.isEmpty(userIdList)){
            return new ArrayList<>();
        }
        return userConvert.mysqls2Dos(userAccountMapper.selectByIds(userIdList));
    }

    @Override
    public List<UserDO> selectList(UserDO userDO) {
        List<User> users = userAccountMapper.selectBySelective(userConvert.do2Mysql(userDO));
        return userConvert.mysqls2Dos(users);
    }

    @Override
    public Integer checkUserBySilentId(String orgId, String silentId) {
        return userAccountMapper.checkUserBySilentId(orgId, silentId);
    }

    @Override
    public List<UserDO> listByCondition(UserDO userDO) {
        return userConvert.mysqls2Dos(userAccountMapper.selectBySelective(userConvert.do2Mysql(userDO)));
    }

    @Override
    public List<UserDO> getUserByPhoneOrEmail(String phone, String email) {
        return userConvert.mysqls2Dos(userAccountMapper.getUserByPhoneOrEmail(phone, email));
    }

    @Override
    public UserDO getUserByOrgIdAndPhoneOrEmail(String orgId, String phone, String email) {
        return userConvert.mysql2Do(userAccountMapper.getUserByOrgIdAndPhoneOrEmail(orgId, phone, email));
    }

    @Override
    public List<UserDO> searchOrgIdBiShare(String orgId, Integer biShare, String keyword) {
        return userConvert.mysqls2Dos(userAccountMapper.searchOrgIdBiShare(orgId, biShare, keyword));
    }

    @Override
    public Integer updateUserAcceptFlag(Integer accept, Integer noAccept) {
        Date now = DataBaseUtil.getDate();
        return userAccountMapper.updateUserAcceptFlag(accept, noAccept, now);
    }

    @Override
    public Integer updateUserInner(Integer innerFlag) {
        Date now = DataBaseUtil.getDate();
        return userAccountMapper.updateUserInnerFlag(innerFlag, now);
    }

    @Override
    public List<UserDO> getUserInnerIsNull() {
        return userAccountMapper.getUserInnerIsNull();
    }

    @Override
    public List<UserDO> getUserListByCreateTimeSort(UserDO userDO) {
        return userConvert.mysqls2Dos(userAccountMapper.getUserListByCreateTimeSort(userConvert.do2Mysql(userDO)));
    }

    @Override
    public List<UserDO> getUserListByUsernameOrEmailOrPhone(String orgId, String username, String phone, String email) {
        return userConvert.mysqls2Dos(userAccountMapper.getUserListByUsernameOrEmailOrPhone(orgId, username, phone, email));
    }

    @Override
    public Integer updateUserByQyWxOpenUserid(String qyWxOpenUserid) {
        // 用于登陆接口没有operatorId
        Date now = DataBaseUtil.getDate();
        return userAccountMapper.updateUserByQyWxOpenUserid(qyWxOpenUserid, now);
    }

    @Override
    public Integer updateByOrgIdSelective(UserDO userDO) {
        User user = userConvert.do2Mysql(userDO);
        user.setOperatorId(user.getUserId());
        DataBaseUtil.setUpdateTime(user);
        return userAccountMapper.updateByOrgIdSelective(user);
    }

    @Override
    public Integer countByOrgId(String orgId) {
        return userAccountMapper.countByOrgId(orgId);
    }

    @Override
    public List<UserDO> pageByNameOrUserNameOrEmail(String orgId, String search, Integer status, List<String> userIds, Integer offset, Integer size) {
        List<User> userList = userAccountMapper.pageByNameOrUserNameOrEmail(orgId, search, status, userIds, offset, size);
        return userConvert.mysqls2Dos(userList);
    }

    @Override
    public Long countByNameOrUserNameOrEmail(String orgId, String search, Integer status, List<String> userIds) {
        return userAccountMapper.countByNameOrUserNameOrEmail(orgId, search, status, userIds);
    }

    @Override
    public List<UserDO> pageByNameOrEmailAndWithGroup(String orgId, String search, Integer status, Integer offset, Integer size) {
        List<User> userList = userAccountMapper.pageByNameOrEmailAndWithGroup(orgId, search, status, offset, size);
        return userConvert.mysqls2Dos(userList);
    }

    @Override
    public Long countByNameOrEmailAndWithGroup(String orgId, String search, Integer status) {
        return userAccountMapper.countByNameOrEmailAndWithGroup(orgId, search, status);
    }

    @Override
    public List<UserDO> getUserByEmailOrPhone(Integer status, String email, String phone) {
        return userConvert.mysqls2Dos(userAccountMapper.getUserByEmailOrPhone(status, email, phone));
    }

    @Override
    public List<UserDO> listUserByOrCondition(UserDO userDO) {
        List<User> users = userAccountMapper.listUserByOrCondition(userConvert.do2Mysql(userDO));
        return userConvert.mysqls2Dos(users);
    }

    @Override
    public Integer userAccountCount(String orgId, Integer status, Integer biShareFlag) {
        return userAccountMapper.userAccountCount(orgId, status, biShareFlag);
    }

    @Override
    public List<UserDO> listUsers(UserDO userDO, List<Integer> statuses, String search, String tableFields) {
        return userAccountMapper.listUsers(userDO, statuses, search, tableFields);
    }

    @Override
    public void unbindWechatWork(String orgId, String openUserid) {
        userAccountMapper.unbindWechatWork(orgId, openUserid, DataBaseUtil.getDate());
    }

    @Override
    public void unbindFeishu(String orgId, String openId) {
        userAccountMapper.unbindFeishu(orgId, openId, DataBaseUtil.getDate());
    }

    @Override
    public List<String> listByRoleIdJoinUserRole(String dstOrgId, String dstAdminRoleId) {
        return userAccountMapper.listByRoleIdJoinUserRole(dstOrgId, dstAdminRoleId);
    }

    @Override
    public UserDO getOneUserByFilter(UserDO userDO) {
        User user = userAccountMapper.getOneUserByFilter(userConvert.do2Mysql(userDO));
        return userConvert.mysql2Do(user);
    }

    @Override
    public List<String> getUserIdsByRoleAndOrgAndStatus(String roleId, String orgId, List<Integer> status) {
        return userAccountMapper.getUserIdsByRoleAndOrgAndStatus(roleId, orgId, status);
    }

    @Override
    public List<UserDO> selectByOrgAndEmailAndUserIds(String orgId, String email, List<String> userIds) {
        List<User> users = userAccountMapper.selectByOrgAndEmailAndUserIds(orgId, email, userIds);
        return userConvert.mysqls2Dos(users);
    }
    @Override
    public List<UserDO> selectByOrgAndPhonePhoneAndUserIds(String orgId, String phone, List<String> userIds) {
        List<User> users = userAccountMapper.selectByOrgAndPhonePhoneAndUserIds(orgId, phone, userIds);
        return userConvert.mysqls2Dos(users);
    }

    @Override
    public void updateByUserIds(List<String> userIds, UserDO user) {
        User param = userConvert.do2Mysql(user);
        param.setUpdateTime(DataBaseUtil.getDate());
        userAccountMapper.updateByUserIds(userIds, param);
    }


    @Override
    public List<UserConditionGroupDO> listUsersByGroupIdAndRoleId(Set<String> groupIdList, Set<String> roleIdList) {
        if (CollectionUtils.isEmpty(groupIdList) || CollectionUtils.isEmpty(roleIdList)) {
            return new ArrayList<>();
        }
        return userAccountMapper.listUsersByGroupIdAndRoleId(groupIdList, roleIdList);
    }


    @Override
    public void unbindWechatByUserId(String userId) {
        if (!ObjectId.isValid(userId)) {
            return;
        }
        userAccountMapper.unbindWechatByUserId(userId);
    }


    @Override
    public void bindWechat(String userId, String unionId, String offiOpenId, String nickname) {
        if (!ObjectId.isValid(userId)) {
            return;
        }
        userAccountMapper.bindWechat(userId, unionId, offiOpenId, nickname);
    }


    @Nullable
    @Override
    public User getByWxUnionId(String unionId) {
        return userAccountMapper.selectByWxUnionId(unionId);
    }


    @Override
    public void updateInnerFlagByOrgIds(List<String> orgIds, Integer innerFlag) {
        userAccountMapper.updateInnerFlagByOrgIds(orgIds, innerFlag, DataBaseUtil.getDate());
    }


    @Override
    public void unbindWechatWorkByOrgId(String orgId) {
        if (!ObjectId.isValid(orgId)) {
            return;
        }
        userAccountMapper.unbindWechatWorkByOrgId(orgId);
    }

    @Override
    public List<UserDO> listIdsByUserName(Set<String> userNames, String orgId) {

        return userConvert.mysqls2Dos(userAccountMapper.listIdsByUserName(userNames, orgId));
    }


    @Override
    public int updateAccountById(User user) {
        DataBaseUtil.setUpdateTime(user);
        return userAccountMapper.updateAccountById(user);
    }
}
